package com.hlhj.funlike_business.activity.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.mymvp.mvp.BaseFgm;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.adapter.AllOrderAdp;
import com.hlhj.funlike_business.bean.OrderListBean;
import com.hlhj.funlike_business.presenter.CompletedFragmentPresenter;
import com.hlhj.funlike_business.views.CompletedFragmentView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class CompletedFragment extends BaseFgm<CompletedFragmentView,CompletedFragmentPresenter> implements CompletedFragmentView {
    private SpringView spring;
    private RecyclerView recyclerView;
    private int page;
    private AllOrderAdp allOrderAdp;
    private List<OrderListBean.DataBeanX.DataBean> dataBeanList;
    @Override
    public void doBeforInit() {

    }

    @Override
    public int getContentId() {
        return R.layout.fgm_completed;
    }

    @NotNull
    @Override
    public CompletedFragmentView bindView() {
        return this;
    }

    @NotNull
    @Override
    public CompletedFragmentPresenter createPresenter() {
        return new CompletedFragmentPresenter();
    }

    @Override
    public void initView() {
        spring=rootView.findViewById(R.id.spring);
        recyclerView=rootView.findViewById(R.id.recycle_list);
    }

    @Override
    public void logicStart() {
        spring.setHeader(new DefaultHeader(getContext()));
        spring.setHeader(new DefaultFooter(getContext()));
        dataBeanList = new ArrayList<>();
        allOrderAdp=new AllOrderAdp(dataBeanList);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        recyclerView.setAdapter(allOrderAdp);

        getPresenter().LoadOrderList(getContext(),40,page,10);
    }

    @Override
    public void setListener() {
        spring.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page=1;
                getPresenter().LoadOrderList(getContext(),40,page,10);
                spring.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                page++;
                getPresenter().LoadOrderList(getContext(),40,page,10);
                spring.onFinishFreshAndLoad();
            }
        });
    }

    @Override
    public void showOrderList(List<OrderListBean.DataBeanX.DataBean> dataBeans) {
        if (page==1){
            dataBeanList.clear();
            dataBeanList.addAll(dataBeans);
        }else {
            dataBeanList.addAll(dataBeans);
        }
        allOrderAdp.notifyDataSetChanged();
    }
}
